apiready = function(){
seajs.use(['../../V',"../../api","../../jquery","../../app","../../service"],function(V,$api,$,app,service){
	var vm = {
		el: '.container',
	  	data: {
				templateList: [1],
				min_goods_amount: [],
				amount: [],
				start_time: '',
				end_time: '',
	  	},
	  	mounted:function() {

	  	},
	  	methods:{
				addTemplateList: function(){
					this.templateList.push('1');
				},
				delTemplateList: function(index){
					if(index>0){
						this.templateList.splice(index, 1);
						this.min_goods_amount.splice(index, 1);
						this.amount.splice(index, 1);
					}else{
						api.toast({
								msg: '不能再删除了...',
								duration: 2000,
								location: 'middle'
						});
					}

				},
				//时间位数为1位数时，前面补0
				setFormat: function (x) {
				  if (x < 10) {
				    x = "0" + x;
				  }
				  return x;
				},
				startTime: function(){
					var that = this;
					var dtpicker = new mui.DtPicker({
					    "type": "date",
					})
					dtpicker.show(function(rs) {
						if($('#endTime').html() && rs.text >= $('#endTime').html()){
							api.toast({
							    msg: '开始时间必须小于结束时间...',
							    duration: 2000,
							    location: 'middle'
							});
							return
						}else{
							var now = app.getCurentTime();
							console.log(JSON.stringify(rs));

							// 获取今天的 年月日 日期
							var timestamp2 = new Date(now.replace(/-/g,   "/")); //转换成Data();
							Y = timestamp2.getFullYear() + '-';
							M = (timestamp2.getMonth()+1 < 10 ? '0'+(timestamp2.getMonth()+1) : timestamp2.getMonth()+1) + '-';
							D = that.setFormat(timestamp2.getDate());
							var startTimeString = Y+M+D;

							if(rs.text < startTimeString){
								api.toast({
								    msg: '不能选择过去的时间...',
								    duration: 2000,
								    location: 'middle'
								});
								return
							}else{
								document.getElementById('startTime').innerHTML = rs.text;
								that.start_time = rs.text;
						    console.log(JSON.stringify(rs));
								dtpicker.dispose()
							}
						}
					})

				},
				endTime: function(){
					var that = this;
					var dtpicker = new mui.DtPicker({
							"type": "date",
					})
					dtpicker.show(function(rs) {
						if($('#startTime').html() && rs.text <= $('#startTime').html()){
							api.toast({
							    msg: '结束时间必须大于开始时间...',
							    duration: 2000,
							    location: 'middle'
							});
							return
						}else{
							var now = app.getCurentTime();
							if(rs.text < now){
								api.toast({
								    msg: '不能选择过去的时间...',
								    duration: 2000,
								    location: 'middle'
								});
								return
							}else{
								document.getElementById('endTime').innerHTML = rs.text;
								that.end_time = rs.text;
								console.log(JSON.stringify(rs));
								dtpicker.dispose()
							}
						}

					})

				},
				saveActivity: function(){
					var active_name = $api.trim(this.$refs.active_name.value);	//活动名称
					var start_time = $('#startTime').html();	//开始时间
					var end_time = $('#endTime').html();	//结束时间


					var min_goods_amount = this.min_goods_amount;	//满足条件的最小金额
					var amount = this.amount;	//满足条件后减免的金额

					if (active_name == '' || active_name == null) {
						api.toast({
								msg: '请输入活动名称...',
								duration: 2000,
								location: 'middle'
						});
						return;
					}
					if (start_time == '' || start_time == null || start_time == '选择日期时间 ...') {
						api.toast({
								msg: '请输入开始时间...',
								duration: 2000,
								location: 'middle'
						});
						return;
					}
					if (end_time == '' || end_time == null || end_time == '选择日期时间 ...') {
						api.toast({
								msg: '请输入结束时间...',
								duration: 2000,
								location: 'middle'
						});
						return;
					}
					for(var i=0;i<min_goods_amount.length;i++){
						if($api.trim(min_goods_amount[i]) == '' || parseInt($api.trim(min_goods_amount[i])) < 0 || parseInt($api.trim(min_goods_amount[i])) < parseInt($api.trim(amount[i]))){
							api.toast({
									msg: '请正确填写金额...',
									duration: 2000,
									location: 'middle'
							});
							return
						}
					}
					for(var i=0;i<amount.length;i++){
						if($api.trim(amount[i]) == '' || parseInt($api.trim(amount[i])) < 0 || parseInt($api.trim(min_goods_amount[i])) < parseInt($api.trim(amount[i]))){
							api.toast({
									msg: '请正确填写金额...',
									duration: 2000,
									location: 'middle'
							});
							return
						}
					}
					if((min_goods_amount.length==0 || min_goods_amount.length==0) || (min_goods_amount.length != amount.length)){
						api.toast({
								msg: '请正确填写金额...',
								duration: 2000,
								location: 'middle'
						});
						return
					}

					//--转换数组为接口所需格式
					var coupon_arr = [];
						for(var i=0;i<min_goods_amount.length;i++){
							coupon_arr.push({
								'amount': amount[i],
								'min_goods_amount': min_goods_amount[i]
							})
						}

						app.ajax(service['setCouponActive'],
						{
							data: {
								values: {
									shop_code: api.pageParam.shopId,
									unionid: $api.getStorage("shop_unionid"),
									active_name: active_name,
									start_time: start_time,
									end_time: end_time,
									coupon: JSON.stringify(coupon_arr),
								}
							}
						},
						function (res) {
							// var shopCode = res.data.shop_code; // 商店id
							if(res.code){
								alert("错误号码code："+res.code+"\n错误信息："+res.message)
							}else{
								console.log(res.data)
								alert('添加店铺活动成功！')

								api.sendEvent({
								    name: 'refresh',
								    extra: {}
								});
								api.closeWin();

							}
						}
					);

					// console.log(coupon_arr.length+', '+JSON.stringify(coupon_arr)+', '+coupon_arr.constructor)
					// console.log(min_goods_amount.length+', '+JSON.stringify(min_goods_amount)+', '+min_goods_amount.constructor)
				},
	  	},
	  	created:function() {
				var that = this;


	  	}
	};
	var even = function() {}
	var newVue = new Vue(vm);
	even();
});
}
